home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
8_2.lha
/
8_2
/
8_2a.c
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
1KB
|
74 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Exercise 8.2
truct name_and_address
char **name;
int len;
name_and_address()
{
name = new char*[1];
name[0] = 0;
len = 1;
}
// construct an address using
// another as its initializer
name_and_address(name_and_address &na)
{
// init name and pass the buck to op=
name = 0;
*this = na;
}
// A helper function to remove the strings.
// This is used by assignment and input.
void deallocate_name();
// assign one address to another
name_and_address& operator=(name_and_address &na)
{
// delete any strings already there
deallocate_name();
// allocate and copy the strings
name = new char*[na.len];
for (char **tmpname = name, **tmpnaname = na.name;
*tmpnaname; tmpname++, tmpnaname++)
{
*tmpname = new char[strlen(*tmpnaname) + 1];
strcpy(*tmpname, *tmpnaname);
}
*tmpname = 0;
len = na.len;
return *this;
}
// remove the parts of an address
~name_and_address()
{
deallocate_name();
}
// input and output
friend istream& operator>>
(istream&, name_and_address&);
friend ostream& operator<<
(ostream&, name_and_address&);
;
oid name_and_address::deallocate_name()
if (name)
{
for (char **svname = name; *svname; )
delete *svname++;
delete name;
name = 0;
}